﻿using MINERP.BLL.CustomerManage;
using MINERP.BLL.NewPhotoRindGrid;
using MINERP.ComControls;
using MINERP.MODEL.FoundationModel;
using MINERP.MODEL.NewProject;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MINERP.NewProject
{
    public partial class FrmNewProject : FrmParent
    {
        OrderInfoBLL _orderbll = new OrderInfoBLL();
        public FrmNewProject()
        {
            InitializeComponent();
        }
        GridBLL_Grid gridBll = new GridBLL_Grid();

        public List<string> listCode = new List<string>();

        public void InitialOutShopGrid()
        {
            foreach (Grid_Info grid in listgrid)
            {
                OutScan outscan = new OutScan();
                outscan.Tag = grid;
                outscan.Width = 200;
                outscan.Height = 150;
                outscan.gridname = grid.GridName;
                outscan.existence = false;  
                outscan.Click += outscan_Click;
                if (grid.recordModel != null)
                {

                    if (grid.recordModel.orderinfoModel != null)
                    {
                        outscan.existence = true;
                        outscan.shopName = grid.recordModel.orderinfoModel.ShopName;
                        outscan.ctmName = grid.recordModel.orderinfoModel.CtmName;
                        outscan.orderName = grid.recordModel.orderinfoModel.OrderProductName;
                        outscan.orderCode = grid.recordModel.orderinfoModel.OrderProductCode;
                        outscan.Num = grid.recordModel.Num;
                      
                        listCode.Add(outscan.orderCode);
                    }
                }
                tableLayoutPanelOutOrder.Controls.Add(outscan);

            }
        }

        void outscan_Click(object sender, EventArgs e)
        {
            OutScan os = sender as OutScan;
            Grid_Info grid = os.Tag as Grid_Info;
            os.BackColor = Color.Red;

            foreach (Control item in tableLayoutPanelOutOrder.Controls)
            {

                OutScan outscan = item as OutScan;
                Grid_Info grinfo = outscan.Tag as Grid_Info;
                if (grid.GridId != grinfo.GridId)
                {
                    outscan.BackColor = Color.PeachPuff;
                }
            }
        }
        List<Grid_Info> listgrid = new List<Grid_Info>();
        private void FrmNewProject_Load(object sender, EventArgs e)
        {
            listgrid = gridBll.GetGridInfoList();
            InitialOutShopGrid();

            com_Type.SelectedIndex = 0;
        }
        //确定
        private void BtnQD_Click(object sender, EventArgs e)
        {
            SaoMiao();
        }
        //清理格子
        private void BtnQL_Click(object sender, EventArgs e)
        {

            if (DialogResult.OK == MessageBox.Show("确认清理该格子", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {

                int gridid = 0;
                OutScan outsc = new OutScan();
                foreach (Control item in tableLayoutPanelOutOrder.Controls)
                {

                    OutScan outscan = item as OutScan;
                    Grid_Info grinfo = outscan.Tag as Grid_Info;

                    if (outscan.BackColor == Color.Red)
                    {
                        outsc = outscan;
                        gridid = grinfo.GridId;
                        break;
                    }
                }
                string mes = "";

                bool isgrid = gridBll.ClearGridData(gridid, out mes);
                if (isgrid == true)
                {
                    MessageBox.Show("格子清除成功！");
                    outsc.RefreshQC();

                }
                else
                {
                    MessageBox.Show("格子清除失败！" + mes);
                }
            }
        }

        private void txtCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SaoMiao();
            }
        }

        public void SaoMiao() 
        {
            string txtcode = txtCode.Text.Trim();
            if (string.IsNullOrEmpty(txtcode))
            {
                MessageBox.Show("请输入编号");
                return;
            }

            string code = "";
            if (com_Type.Text == "产品编号")
            {
                if (txtcode.Length == 10)
                {
                    code = txtcode;
                }
                else
                {
                    code = "2" + String.Format("{0:D9}", Convert.ToInt32(txtcode));
                }
            }
            else if (com_Type.Text == "照片编号")
            {
                if (txtcode.Length == 10)
                {
                    code = txtcode;
                }
                else
                {
                    code = "3" + String.Format("{0:D9}", Convert.ToInt32(txtcode));
                }
            }
            else
            {

            }

            int gzid = 0;

            txtCode.Text = code;            
            txtCode.SelectAll();
            string mess = "";

            OrderInfoProject listodinfo = _orderbll.ProjectSM(code, base.userId, out gzid, out mess);

            if (listodinfo == null || listodinfo.gridId==0)
            {
                lbl_mess.Text = mess;
                return;
            }
            else
            {

            }
            //改格子里面的信息
            if (listCode.Contains(code))
            {
                foreach (Control item in tableLayoutPanelOutOrder.Controls)
                {
                    OutScan outscan = item as OutScan;
                    Grid_Info grinfo = outscan.Tag as Grid_Info;
                    if (outscan.existence && grinfo.GridId == gzid)
                    {
                        outscan.Num = listodinfo.Num;
                        outscan.RefreshNum();
                        break;
                    }
                }
            }
            else
            {

                foreach (Control item in tableLayoutPanelOutOrder.Controls)
                {
                    OutScan outscan = item as OutScan;
                    Grid_Info grinfo = outscan.Tag as Grid_Info;
                    if (grinfo.GridId == gzid)
                    {
                        outscan.existence = true;

                        outscan.shopName = listodinfo.shopName;
                        outscan.ctmName = listodinfo.CtmName;
                        outscan.orderName = listodinfo.orderName;
                        outscan.orderCode = listodinfo.orderCode;
                        outscan.Num = listodinfo.Num;
                        outscan.RefreshNum();
                        break;
                    }
                }
            }
            //定位到当前格子
            foreach (Control item in tableLayoutPanelOutOrder.Controls)
            {
                OutScan outscan = item as OutScan;
                Grid_Info grinfo = outscan.Tag as Grid_Info;
                if (gzid != grinfo.GridId)
                {
                    outscan.BackColor = Color.PeachPuff;
                }
                else
                {
                    outscan.BackColor = Color.Red;
                }
            }

        }
    }
}
